package com.reading.core.manager.impl;

import com.reading.common.entity.view.BookLabel;
import com.reading.common.entity.view.Label;
import com.reading.common.persistence.view.BookLabelMapper;
import com.reading.common.persistence.view.LabelMapper;
import com.reading.core.exception.AloneException;
import com.reading.core.exception.label.LabelNewBuildFailException;
import com.reading.core.manager.ILabelManager;
import com.reading.core.utils.CheckUtil;
import com.reading.core.utils.GlobalConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author guwenqi_1995@163.com
 * @date 2019/7/10 1:12 PM
 **/
@Slf4j
@Service
public class LabelManagerImpl implements ILabelManager {

    @Resource
    private LabelMapper labelMapper;

    @Resource
    private BookLabelMapper bookLabelMapper;

    @Override
    public void createLabel(String labelName) throws AloneException {
        Label label = new Label(labelName);
        CheckUtil.valid(label);

        if (labelMapper.insert(label) <= GlobalConstant.NUMBER_0) {
            log.error("");
            throw new LabelNewBuildFailException();
        }
    }

    @Override
    public void deleteLabel(Integer labelId) throws AloneException {
        if (labelMapper.delete(labelId) <= GlobalConstant.NUMBER_0) {
            log.error("");
            throw new LabelNewBuildFailException();
        }
    }

    @Override
    public void addBookLabel(Integer bookId, Integer labelId) throws AloneException {
        BookLabel bookLabel = new BookLabel(bookId, labelId);
        CheckUtil.valid(bookLabel);

        CheckUtil.isOk(bookLabelMapper.insert(bookLabel));
    }

    @Override
    public void unaddBookLabel(Integer bookId, Integer labelId) throws AloneException {
        BookLabel bookLabel = new BookLabel(bookId, labelId);
        CheckUtil.valid(bookLabel);

        CheckUtil.isExist(bookLabelMapper.selectContact(bookLabel));
        CheckUtil.isOk(bookLabelMapper.delete(bookLabel));
    }
}
